﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LittleLove.Base;

namespace LittleLove.UC
{
    public partial class ScreenUC : UserControl
    {
        #region Attributs and Getter/Setter
        public static int CurrentYear = 2012;
        private CustomizedToolTip m_ttMonth = new CustomizedToolTip();
        private static Dictionary<int, bool[]> m_isBtnClicked;
        #endregion
        public ScreenUC()
        {
            InitializeComponent();
        }

        private void ScreenUC_Load(object sender, EventArgs e)
        {
            intitializeClickEvents();
            updateBtnVisibility();
            designBtnInterface();
            intitializeBtnClick();
        }

        #region Design buttons
        private void initializeBackgroundMusic()
        {
            WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
            wplayer.URL = @"C:\Users\User\Desktop\MariageDAmour.mp3";
            wplayer.URL = "../../Resource/MariageDAmour.mp3";
            wplayer.controls.play();
        }

        private void intitializeBtnClick()
        {
            m_isBtnClicked = new Dictionary<int, bool[]>();
            for (int year = Base.CalendarDB.FirstYear; year <= Base.CalendarDB.LastYear; year++)
            {
                bool[] arrayBtnClicked = new bool[12] { false, false, false, false, false, false,
                                    false, false, false, false, false, false};
                m_isBtnClicked.Add(year, arrayBtnClicked);
            }
        }

        private void designBtnToolTip()
        {
            m_ttMonth.RemoveAll();
            foreach (Button btnItem in Base.Controls.GetAll(this, typeof(Button)))
            {
                string orderString =
                   System.Text.RegularExpressions.Regex.Match(btnItem.Name, @"\d+").Value;
                // Choose only month button
                if (!String.IsNullOrEmpty(orderString))
                {
                    // Get event information
                    Base.PresentDB.getEvent((Base.PresentDB.Events)(CurrentYear * 100 + Int32.Parse(orderString))); 
                    string month = Base.Convert.numberToMonth(orderString);
                    m_ttMonth.SetToolTip(btnItem, @"28 " + month + " " + CurrentYear.ToString() + @"
" + Base.PresentDB.Title + " Anniversary" + @"
Present:");
                    btnItem.Tag = Properties.Resources.eiffel;
                }
            }
        }

        private void designBtnBackground()
        {
            foreach (Button btnItem in Base.Controls.GetAll(this, typeof(Button)))
            {
                string orderString =
                   System.Text.RegularExpressions.Regex.Match(btnItem.Name, @"\d+").Value;
                // Choose only month button
                if (!String.IsNullOrEmpty(orderString))
                {
                    btnItem.BackgroundImage = Properties.Resources.heart_empty;
                    btnItem.BackgroundImageLayout = ImageLayout.Stretch;
                    btnItem.Font = new System.Drawing.Font("VNI-Thufap3", 10, FontStyle.Bold);
                    btnItem.ForeColor = Color.Red;
                    btnItem.Text = orderString;
                    btnItem.MouseMove += new MouseEventHandler(btnItem_MouseMove);
                    btnItem.MouseLeave += new EventHandler(btnItem_MouseLeave);    
                }
            }
        }

        private void designBtnInterface()
        {
            designBtnToolTip();
            designBtnBackground();
        }
        #endregion

        #region Logic fucntions
        /// <summary>
        /// Update status of button when changing screen
        /// </summary>
        private void updateBtnStatus()
        {
            foreach (Button btnItem in Base.Controls.GetAll(this, typeof(Button)))
            {
                string orderString =
                   System.Text.RegularExpressions.Regex.Match(btnItem.Name, @"\d+").Value;
                // Choose only month button
                if (!String.IsNullOrEmpty(orderString))
                {
                    if (m_isBtnClicked[CurrentYear][Int32.Parse(orderString) - 1])
                    {
                        btnItem.BackgroundImage = Properties.Resources.heart;
                        btnItem.ForeColor = Color.White;
                    }
                    else
                    {
                        btnItem.BackgroundImage = Properties.Resources.heart_empty;
                        btnItem.ForeColor = Color.Red;
                    }
                }
            }
        }

        /// <summary>
        /// Update visibility of button when changing screen
        /// </summary>
        private void updateBtnVisibility()
        {
            if (CurrentYear > Base.CalendarDB.FirstYear && CurrentYear < Base.CalendarDB.LastYear)
            {
                foreach (Button btnItem in Base.Controls.GetAll(this, typeof(Button)))
                {
                    btnItem.Visible = true;
                }
            }
            else
            {
                if (CurrentYear == Base.CalendarDB.FirstYear)
                {
                    btnPrevious.Visible = btnMonth1.Visible = false;
                }
                if (CurrentYear == Base.CalendarDB.LastYear)
                {
                    btnNext.Visible = btnMonth10.Visible = btnMonth11.Visible 
                        = btnMonth12.Visible = false;
                }
            }
        }

        /// <summary>
        /// Update screen when changing year
        /// </summary>
        private void updateScreen()
        {
            // Change background
            this.BackgroundImage =
                Base.CalendarDB.getScreen((CalendarDB.Years)(CurrentYear)).BackgroundImage;  
            updateBtnVisibility();
            designBtnToolTip();
            updateBtnStatus();
        }

        /// <summary>
        /// Convert from CurrentYear and button order to index
        /// </summary>
        /// <param name="sender"></param>
        private void convertMonthYear(object sender)
        {
            Button tb = (Button)sender;
            // Get last number from RadioButton name
            int order =
                    Int32.Parse(System.Text.RegularExpressions.Regex.Match(tb.Name, @"\d+").Value);
            // Set status of button; -1 beacause array begins by zero
            m_isBtnClicked[CurrentYear][order - 1] = true;
            // Show slides
            SlideShowForm slideForm =
                new SlideShowForm((Base.PresentDB.Events)(CurrentYear * 100 + order));
            slideForm.ShowDialog();
        }

        #endregion

        #region Click events
        private void intitializeClickEvents()
        {
            foreach (Button btnItem in Base.Controls.GetAll(this, typeof(Button)))
            {
                string orderString =
                   System.Text.RegularExpressions.Regex.Match(btnItem.Name, @"\d+").Value;
                // Choose only month button
                if (!String.IsNullOrEmpty(orderString)) 
                {
                    btnItem.Click += new EventHandler(btnItem_Click);
                }
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            CurrentYear++;
            updateScreen();          
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            CurrentYear--;
            updateScreen();
        }
        
        private void btnItem_Click(object sender, EventArgs e)
        {
            convertMonthYear(sender);
        }

        private void btnItem_MouseMove(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            btn.BackgroundImage = Properties.Resources.heart;
            btn.ForeColor = Color.White;
        }

        private void btnItem_MouseLeave(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            // Get order of button
            int order =
                    Int32.Parse(System.Text.RegularExpressions.Regex.Match(btn.Name, @"\d+").Value);
            // Change status of button if it is not clicked
            if (!m_isBtnClicked[CurrentYear][order - 1])
            {
                btn.BackgroundImage = Properties.Resources.heart_empty;
                btn.ForeColor = Color.Red;
            }
        }
        #endregion
    }
}
